package com.core.zyyz.pcmc.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.core.common.utils.StringUtils;
import com.core.zyyz.pcmc.domain.PcmcDept;
import com.core.zyyz.pcmc.service.PcmcDeptService;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.common.utils.poi.ExcelUtil;
import com.core.common.core.page.TableDataInfo;

/**
 * 机构Controller
 *
 * @author core
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/pcmc/dept")
public class PcmcDeptController extends BaseController
{
    @Autowired
    private PcmcDeptService pcmcDeptService;

    /**
     * 查询机构列表
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(PcmcDept pcmcDept)
    {
        startPage();
        List<PcmcDept> list = pcmcDeptService.selectPcmcDeptList(pcmcDept);
        return getDataTable(list);
    }

    /**
     * 导出机构列表
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:export')")
    @Log(title = "机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmcDept pcmcDept)
    {
        List<PcmcDept> list = pcmcDeptService.selectPcmcDeptList(pcmcDept);
        ExcelUtil<PcmcDept> util = new ExcelUtil<PcmcDept>(PcmcDept.class);
        util.exportExcel(response, list, "机构数据");
    }

    /**
     * 获取机构详细信息
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:query')")
    @GetMapping(value = "/{deptCode}")
    public AjaxResult getInfo(@PathVariable("deptCode") String deptCode)
    {
        return success(pcmcDeptService.selectPcmcDeptByDeptCode(deptCode));
    }

    /**
     * 新增机构
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:add')")
    @Log(title = "机构", businessType = BusinessType.INSERT)
    @PostMapping("/insertDept")
    public AjaxResult add(@RequestBody PcmcDept pcmcDept)
    {
        return toAjax(pcmcDeptService.insertPcmcDept(pcmcDept));
    }

    /**
     * 修改机构
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:edit')")
    @Log(title = "机构", businessType = BusinessType.UPDATE)
    @PostMapping("/updataDept")
    public AjaxResult edit(@RequestBody PcmcDept pcmcDept)
    {
        return toAjax(pcmcDeptService.updatePcmcDept(pcmcDept));
    }

    /**
     * 删除机构
     */
    @PreAuthorize("@ss.hasPermi('pcmc:dept:remove')")
    @Log(title = "机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptCodes}")
    public AjaxResult remove(@PathVariable String[] deptCodes)
    {
        return toAjax(pcmcDeptService.deletePcmcDeptByDeptCodes(deptCodes));
    }

    /**
     * 查询机构附加属性
     * @param paraMap
     * @return
     */
    @GetMapping({"/query/attribute"})
    public TableDataInfo queryDeptAttribute(@RequestParam Map<String, Object> paraMap) {
       int colSize = MapUtils.getInteger(paraMap, "colSize", Integer.valueOf(1)).intValue();
       String isInfo = MapUtils.getString(paraMap, "isInfo", "0");
       List<Map<String, Map<String, Object>>> list = this.pcmcDeptService.queryDeptAttribute(colSize, isInfo);
       return getDataTable(list);
    }

    /**
     * 查询机构
     * @param paraMap
     * @return
     */
    @GetMapping({"/query/unique"})
    public AjaxResult queryPcmcDept(@RequestParam Map<String, Object> paraMap) {
        String deptCode = MapUtils.getString(paraMap, "deptCode");
        if (StringUtils.isBlank(deptCode)) {
            return success(Maps.newHashMap());
        }
        String isInfo = MapUtils.getString(paraMap, "isInfo", "0");
        PcmcDept dept = pcmcDeptService.selectPcmcDeptByDeptCode(deptCode);
        if (ObjectUtil.isNotNull(dept)) {
            List<Map<String, Map<String, Object>>> list = this.pcmcDeptService.queryDeptAttribute(1, isInfo);
            boolean isShowAttribute = CollectionUtils.isNotEmpty(list);
            dept.setShowAttribute(Boolean.valueOf(isShowAttribute));
        }

       return success(dept);
    }
}
